<template>
  <div class="mainsu">
    <div class="main_spac1">
      <div class="main_white padding10 paddingBottom20">
        <div class="main_crumbs boxdq">
          <div class="main_crumbs_l">
            <a href="javascript:;" @click="$router.back(-1)">
              <i class="el-icon-arrow-down"></i>返回
            </a>
            <em>|</em>
            <span>询价记录</span>
          </div>
        </div>

        <div class="mainor_form batch-wrap form-item-mg mg-t-14">
          <el-form
            :inline="true"
            :model="form"
            class="demo-form-inline"
            ref="userListFrom"
            label-width="90px"
          >
            <el-form-item label="采购日期：" prop="purchase_time">
              <el-date-picker
                v-model="form.purchase_time"
                type="date"
                class="wid220"
                :editable="false"
                placeholder="选择采购日期"
                value-format="yyyy-MM-dd"
                :clearable="false"
              />
            </el-form-item>

            <el-form-item label="商品分类：" prop="category_id" class="wid220">
              <el-cascader
                v-model="form.category_id"
                :options="options"
                :props="defaultProps"
              ></el-cascader>
            </el-form-item>

            <el-form-item label="商品名称：" prop="goods_name">
              <el-input
                v-model.trim="form.goods_name"
                placeholder="商品名称"
                clearable
                class="wid220"
              ></el-input>
            </el-form-item>
          </el-form>
          <div class="">
            <el-button
              class="reset mg-r-6"
              type="primary"
              @click="reset"
              >重置</el-button
            >
            <el-button
              type="primary"
              @click="getArticleList"
              :loading="listLoading"
              >搜索</el-button
            >
          </div>
        </div>

        <div class="main_table" style="min-height: 750px">
          <el-table
            ref="multipleTable"
            v-loading="listLoading"
            :data="list.list"
            element-loading-text="加载中"
            fit
            border
            highlight-current-row
            :row-class-name="getRowClass"
            @expand-change="expandChange"
          >
            <el-table-column type="expand" class-name="main_hei">
              <template slot-scope="props">
                <el-table
                  :ref="'expandchild' + props.$index"
                  :data="props.row.order_list"
                  class="mainex_ta"
                  fit
                  :row-class-name="getRowClass"
                  :highlight-current-row="false"
                >
                  <el-table-column width="40" align="right" />
                  <el-table-column
                    label="采购员"
                    :show-overflow-tooltip="true"
                    prop="buyer_name"
                    min-width="100"
                  />
                  <el-table-column
                    label="供应商类型"
                    prop="supplier_type"
                    show-overflow-tooltip
                    min-width="100"
                  />
                  <el-table-column
                    label="供应商名称"
                    prop="supplier_name"
                    show-overflow-tooltip
                    min-width="100"
                  />
                  <el-table-column
                    label="报价"
                    prop="price"
                    show-overflow-tooltip
                  />
                </el-table>
              </template>
            </el-table-column>

            <el-table-column
              label="商品名称"
              prop="goods_name"
              show-overflow-tooltip
              min-width="110"
            />
            <el-table-column label="单位" prop="unit" show-overflow-tooltip />

            <el-table-column
              label="描述"
              prop="goods_describe"
              show-overflow-tooltip
              min-width="110"
            />
            <el-table-column
              label="采购日期"
              prop="purchase_time"
              show-overflow-tooltip
            >
              <template slot-scope="scope">{{
                parseTime(scope.row.purchase_time, "{y}-{m}-{d}")
              }}</template>
            </el-table-column>

            <el-table-column
              label="询价结果"
              prop="count"
              show-overflow-tooltip
            >
              <template slot-scope="scope">
                <div
                  style="color: rgb(83, 168, 255)"
                  v-if="scope.row.count != ''"
                >
                  已有{{ scope.row.count }}家报价
                </div>
                <div style="color: rgb(83, 168, 255)" v-else>暂无报价</div>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { inquiry_info } from "@/api/purchasing/purchasingPurchase";
import { apiUrl, objKeySort, Md5 } from "@/utils/public";
import { recursion, category } from "@/utils/index";
import { parseTime, exportMethod } from "@/utils/index";
export default {
  components: {},
  data() {
    return {
      parseTime,
      exportMethod,
      // 分页
      current: {
        page: 1,
        per_page: 20,
      },
      limit: 20,
      totalCount: 1,
      // 列表数据
      list: {
        list: [],
        region: {},
        group: {},
      },
      listLoading: false,
      // 搜索
      form: {
        pid: "",
        category_id: [],
        purchase_time: parseTime(new Date(), "{y}-{m}-{d}"),
        send_time_id: null,
        goods_name: "",
      },
      // 分类
      options: [],
      defaultProps: {
        children: "selfNext",
        label: "title",
        value: "id",
        checkStrictly: true,
      },
    };
  },
  created() {
    // 列表
    this.inquiry_info();
  },
  methods: {
    // 时间

    // 判断表格是否有子项，无子项不显示展开按钮
    getRowClass(row) {
      row.row.index = row.rowIndex;
      if (row.row.order_list) {
        if (row.row.order_list.length === 0) {
          return "row-expand-cover";
        }
      }
    },
    // 监听展开行
    expandChange(rows) {
      if (rows.checked) {
        rows.order_list.forEach((chil) => {
          setTimeout(() => {
            if (chil.checked) {
              if (this.$refs["expandchild" + rows.index]) {
                this.$refs["expandchild" + rows.index].toggleRowSelection(
                  chil,
                  true
                );
              }
            }
          });
        });
      }
    },
    // 监听一级是否勾选
    onTableSelect(rows, row) {
      // console.log(rows, row)
      row.checked = !row.checked;
      if (row.checked) {
        row.order_list.forEach((m) => {
          if (this.$refs["expandchild" + row.index]) {
            this.$refs["expandchild" + row.index].toggleRowSelection(m, true);
          }
          m.checked = true;
        });
      } else {
        row.order_list.forEach((i) => {
          if (this.$refs["expandchild" + row.index]) {
            this.$refs["expandchild" + row.index].clearSelection();
          }
          i.checked = false;
        });
      }
    },
    // 监听二级是否勾选
    expandSelect(rows, row, index) {
      row.checked = !row.checked;
      if (rows.length != 0) {
        this.list.list[index].checked = true;
        this.$refs.multipleTable.toggleRowSelection(
          this.list.list[index],
          true
        );
      } else {
        this.list.list[index].checked = false;
        this.$refs.multipleTable.toggleRowSelection(
          this.list.list[index],
          false
        );
      }
    },
    // 搜索
    getArticleList() {
      this.current.page = 1;
      this.inquiry_info();
    },
    reset() {
      this.$refs["userListFrom"].resetFields();
      this.getArticleList();
    },
    // 分页
    changeLimit(pageSize) {
      this.current.per_page = pageSize;
      this.inquiry_info();
    },
    changePage(page) {
      this.current.page = page;
      this.inquiry_info();
    },
    // 列表
    inquiry_info() {
      this.listLoading = true;
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(
              Object.assign(apiUrl().name, this.list_form(), this.current, {
                status: this.activeName,
              })
            ) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        this.list_form(),
        this.current,
        { status: this.activeName }
      );
      inquiry_info(data).then((res) => {
        this.listLoading = false;
        if (res.errcode == 0) {
          this.options = recursion(res.data.categoryList, true);
          res.data.list.map((e) => {
            e.checked = false;
            // e.order_list.map(p => {
            //   p.checked = false;
            // });
          });
          this.list = res.data;
          this.totalCount = res.data.list.total;
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    list_form() {
      let form = JSON.parse(JSON.stringify(this.form));
      form.pid = form.category_id[0] ? form.category_id[0] : "";
      form.category_id = category(form.category_id);

      return form;
    },
    // 导出明细
    export_detail() {
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name, this.list_form())) +
              apiUrl().appsecret
          ),
        },
        apiUrl().name,
        this.list_form()
      );
      const notify = this.$notify.info({
        title: "开始导出...",
        type: "success",
        duration: 0,
      });
      orderSummary_export_detail(data)
        .then((res) => {
          this.exportMethod(res, "订单明细", notify);
        })
        .catch(() => {
          this.$message.error("导出失败");
        });
    },
    // 导出汇总
    export_all() {
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name, this.list_form())) +
              apiUrl().appsecret
          ),
        },
        apiUrl().name,
        this.list_form()
      );
      const notify = this.$notify.info({
        title: "开始导出...",
        type: "success",
        duration: 0,
      });
      orderSummary_export_all(data)
        .then((res) => {
          this.exportMethod(res, "订单汇总", notify);
        })
        .catch(() => {
          this.$message.error("导出失败");
        });
    },
  },
};
</script>
<style lang="scss" scoped>
@import "@/styles/variables.scss";

.mainsu {
  .mainor_form form {
    width: calc(100% - 220px);
  }
  .mainor_ter {
    margin-top: 4px;
    /deep/.el-button {
      margin-left: 5px;
    }
    a {
      white-space: nowrap;
      border: 1px solid $menuActiveText;
      background-color: $menuActiveText;
      padding: 7px 10px;
      color: $white;
      -webkit-appearance: none;
      text-align: center;
      -webkit-box-sizing: border-box;
      box-sizing: border-box;
      -webkit-transition: 0.1s;
      transition: 0.1s;
      font-weight: 500;
      font-size: $font_14;
      border-radius: 4px;
    }
  }
}
</style>
